﻿using NCM_MSTest.Alg;

namespace NCM_MSTest.Test
{
    [TestClass]
    [TestCategory("最小二乘拟合测试")]
    public class 最小二乘拟合测试
    {
        [TestMethod("线性最小二乘拟合测试")]
        [DataRow(
            new double[] { 4.0, 8.0, 12.5, 16.0, 20.0, 25.0, 31.0, 35.0, 40.0, 40.0 }
            , new double[] { 3.7, 7.8, 12.1, 15.6, 19.8, 24.5, 31.1, 35.5, 39.4, 39.5 }
            , new double[] { 3.7032, 7.7130, 12.2240, 15.7325, 19.7423, 24.7545, 30.7692, 34.7790, 39.7912, 39.7912 }
            , -0.3066, 1.0024, 0.3293, 0.9997
            )]
        public void 线性最小二乘拟合测试(double[] x, double[] y
            , double[] yest, double a, double b, double sigma, double r)
        {
            double[] ryest = new double[yest.Length];
            double ra = 0, rb = 0, rsigma = 0, rr = 0;
            LeastSquaresFit.LinearFit(x, y, ref ryest, ref ra, ref rb, ref rsigma, ref rr);

            System.Diagnostics.Trace.WriteLine("直线最小二乘拟合");
            System.Diagnostics.Trace.WriteLine("i \t x0(i) \t y0(i) \t a+b*x0(i) \t a+b*x0(i)[输出] \t diff");
            for (int i = 0; i < x.Length; i++)
            {
                System.Diagnostics.Trace.WriteLine($"{i + 1} \t {x[i]:f4} \t {y[i]:f4} \t {yest[i]:f4} \t {ryest[i]:f4} \t {yest[i] - ryest[i]:f4}");
            }
            System.Diagnostics.Trace.WriteLine($"预期 a={a:f4} \t b={b:f4} \t sigma={sigma:f4} \t r={r:f4}");
            System.Diagnostics.Trace.WriteLine($"输出 a={ra:f4} \t b={rb:f4} \t sigma={rsigma:f4} \t r={rr:f4}");
            System.Diagnostics.Trace.WriteLine($"差异 a={a - ra:f4} \t b={b - rb:f4} \t sigma={sigma - rsigma:f4} \t r={r - rr:f4}");

            Assert.IsTrue(a.EqualsEx(ra));
            Assert.IsTrue(b.EqualsEx(rb));
            Assert.IsTrue(sigma.EqualsEx(rsigma));
            Assert.IsTrue(r.EqualsEx(rr));
        }
    }
}
